// FriendController.java
package com.baoyouqun.API.friend;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONObject;
import com.baoyouqun.API.friend.domain.*;
import com.baoyouqun.base.PageResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/friend")
@Api(tags = "好友管理接口")
public class FriendController {

    @Resource
    private FriendService friendService;

    @Resource
    private FriendGroupService friendGroupService;

    // 在FriendController中添加以下方法
    @GetMapping("/groups")
    @ApiOperation("获取所有好友分组")
    public MultiResponse<FriendGroupVO> getFriendGroups() {
        return friendGroupService.getFriendGroups();
    }

    @PostMapping("/group/add")
    @ApiOperation("新增好友分组")
    public SingleResponse<String> addFriendGroup(@Valid @RequestBody AddFriendGroupCmd cmd) {
        return friendGroupService.addFriendGroup(cmd);
    }

    @PostMapping("/group/update")
    @ApiOperation("编辑好友分组")
    public Response updateFriendGroup(@Valid @RequestBody UpdateFriendGroupCmd cmd) {
        return friendGroupService.updateFriendGroup(cmd);
    }

    @DeleteMapping("/group/delete/{groupId}")
    @ApiOperation("删除好友分组")
    public Response deleteFriendGroup(@PathVariable String groupId) {
        return friendGroupService.deleteFriendGroup(groupId);
    }

    @GetMapping("/getFriendStatus")
    @ApiOperation("获取好友状态")
    public SingleResponse<JSONObject> getFriendStatus(String userId) {
        StpUtil.checkLogin();
        return friendService.getFriend(userId);
    }

    @GetMapping("/list")
    @ApiOperation("获取好友列表")
    public MultiResponse<FriendVO> getFriendList() {
        StpUtil.checkLogin();
        return friendService.getFriendList();
    }

    @GetMapping("/search")
    @ApiOperation("搜索好友")
    public MultiResponse<FriendVO> searchFriends(@RequestParam String keyword) {
        StpUtil.checkLogin();
        FriendSearchQuery query = new FriendSearchQuery();
        query.setKeyword(keyword);
        return friendService.searchFriends(query);
    }

    @PostMapping("/add")
    @ApiOperation("添加好友")
    public Response addFriend(@Valid @RequestBody AddFriendCmd cmd) {
        StpUtil.checkLogin();
        return friendService.addFriend(cmd);
    }

    @GetMapping("/applications")
    @ApiOperation("获取好友申请列表")
    public MultiResponse<FriendApplicationVO> getFriendApplications() {
        StpUtil.checkLogin();
        return friendService.getFriendApplications();
    }

    @PostMapping("/handleApplication")
    @ApiOperation("处理好友申请")
    public Response handleFriendApplication(@RequestBody HandleFriendApplicationCmd cmd) {
        StpUtil.checkLogin();
        return friendService.handleFriendApplication(cmd);
    }

    @PutMapping("/remark")
    @ApiOperation("更新好友备注")
    public Response updateFriendRemark(@RequestBody UpdateFriendRemarkCmd cmd) {
        StpUtil.checkLogin();
        return friendService.updateFriendRemark(cmd);
    }

    @DeleteMapping("/delete/{friendId}")
    @ApiOperation("删除好友")
    public Response deleteFriend(@PathVariable String friendId) {
        StpUtil.checkLogin();
        return friendService.deleteFriend(friendId);
    }


    @GetMapping("/public/list")
    @ApiOperation("分页查询公开豹友列表")
    public PageResponse<PublicFriendVO> getPublicFriendList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Double latitude,
            @RequestParam(required = false) Double longitude,
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city) {
        StpUtil.checkLogin();

        PublicFriendQuery query = new PublicFriendQuery();
        query.setPageIndex(page);
        query.setPageSize(size);
        query.setProvince(province);
        query.setCity(city);
        query.setLatitude(latitude);
        query.setLongitude(longitude);

        return friendService.getPublicFriendList(query);
    }

    // 新增接口2: 附近豹友数量查询接口
    @GetMapping("/nearby/count")
    @ApiOperation("根据地理位置查询附近豹友数量")
    public SingleResponse<Integer> getNearbyFriendCount(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "10") Double radius) {
        StpUtil.checkLogin();

        NearbyFriendQuery query = new NearbyFriendQuery();
        query.setLatitude(latitude);
        query.setLongitude(longitude);
        query.setRadius(radius);

        return friendService.getNearbyFriendCount(query);
    }

    // 新增接口3: 搜索公开豹友接口
    @GetMapping("/public/search")
    @ApiOperation("根据关键词搜索公开豹友")
    public PageResponse<PublicFriendVO> searchPublicFriends(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        StpUtil.checkLogin();

        PublicFriendQuery query = new PublicFriendQuery();
        query.setKeyword(keyword);
        query.setPageIndex(page);
        query.setPageSize(size);

        return friendService.searchPublicFriends(query);
    }

}